<?php
namespace logic\admin\index;

use model\Admin;
use model\AdminRole;
use think\Db;

/**
 * User: zj
 * Date: 2017/4/26
 * Time: 15:11
 * Progress: 90%
 */
class LAdmin
{
    //管理员列表
    public function index_data($param){
        $model = new Admin();
        $where=array();
        if (!empty($keywords)) {
            $where['nick'] = array('like', "%{$keywords}%"); //查询条件
        }
        $total=$model->where($where)->count(); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['reg_time'] = 'asc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model->alias('a')
            ->join('admin_role r','r.id = a.role_id','left')
            ->field('a.*,r.name as role_name')
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;

    }

    //管理员添加
    public function insert_data($param){

        $model = new Admin();
        $res=$model->validate(true)->save($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'添加成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }

    //编辑页面
    public function edit_data($id){
        $model=new Admin();
        $data=$model->where(['id'=>$id])->find();
        return $data;
    }

    //管理员更新
    public function  update_data($id,$param){
        $model = new Admin();
        $res = $model->validate("Admin.edit")->save($param,['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'修改成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }

    //管理员删除
    public function delete_data($id){
        $model = new Admin();
        $res = $model->where(['id'=>$id])->delete();
        if($res){
            return array(
                'success'=>true,
                'message'=>'删除成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()

            );
        }
    }

    public function role_index_data($param){
        $model = new AdminRole();
        $where=array();
        if (!empty($keywords)) {
            $where['nick'] = array('like', "%{$keywords}%"); //查询条件
        }
        $total=$model->where($where)->count(); //记录总数
        $page_size =$param['limit']; //获取分页数
        $start = $param['offset']; //获取开始的记录数
        //获取排序字段------------------------------------------------------------开始

        $sort_name = $param['ordinal'];
        $sort_desc = $param['order'];

        if (!empty($sort_name)) {
            $sort[$sort_name] = $sort_desc;
        } else {
            $sort['id'] = 'asc'; //默认排序
        }
        //获取排序字段------------------------------------------------------------结束

        $rows = $model
            ->where($where)
            ->limit($start, $page_size)
            ->order($sort)
            ->select();

        $array=array(
            'total'=>$total,
            'rows'=>$rows
        );

        return $array;
    }

    public function get_admin_data($id){
        $model=new Admin();
        return $model->where(['id'=>$id])->find();
    }

    public function admin_role_update($id,$param){
        $model=new Admin();
        $res=$model->where(['id'=>$id])->update($param);
        if($res){
            return array(
                'success'=>true,
                'message'=>'分配角色成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>$model->getError()
            );
        }
    }

    public function change_password_save($id,$param){
        $model=new Admin();
        $admin=$model->where(['id'=>$id])->find();
        if($param['new_password'] != $param['confirm_password']){
            return array(
                'success'=>false,
                'message'=>'两次密码不一致'
            );
        }
        $res=$model->save(['password'=>$param['new_password']],['id'=>$id]);
        if($res){
            return array(
                'success'=>true,
                'message'=>'更改密码成功'
            );
        }else{
            return array(
                'success'=>false,
                'message'=>'更改密码失败'
            );
        }

    }



}